package com.neusoft.a132wxyorderingdish.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.adapter.FoodInfoAdapter;
import com.neusoft.a132wxyorderingdish.beans.CollectResultBean;
import com.neusoft.a132wxyorderingdish.beans.CommentBean;
import com.neusoft.a132wxyorderingdish.beans.FoodBean;
import com.neusoft.a132wxyorderingdish.beans.ResultBean;
import com.neusoft.a132wxyorderingdish.common.Constants;
import com.neusoft.a132wxyorderingdish.listener.RetrofitListener;
import com.neusoft.a132wxyorderingdish.model.ShopModel;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodInfoActivity extends BaseActivity implements RetrofitListener {
    private ImageView foodinfo_pic;
    private TextView foodinfo_name,foodinfo_intro,foodinfo_price;

    private ImageButton imageButton,imageButton1;
    private String result;
    private FoodInfoAdapter foodInfoAdapter;
    private RecyclerView foodinfo_comments;


    @Override
    void initViews() {
        setLayout_file(R.layout.activity_food_info);
        foodinfo_pic=(ImageView)findViewById(R.id.foodinfo_img);
        foodinfo_name=(TextView)findViewById(R.id.foodinfo_foodname);
        foodinfo_intro=(TextView)findViewById(R.id.foodinfo_type);
        foodinfo_price=(TextView)findViewById(R.id.foodinfo_price);
        imageButton=(ImageButton)findViewById(R.id.foodinfo_button);
        imageButton1=(ImageButton)findViewById(R.id.foodinfo_back);

        foodinfo_comments=findViewById(R.id.foodinfo_recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(FoodInfoActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        foodinfo_comments.setLayoutManager(layoutManager);

    }

    @Override
    void initEvents() {
        imageButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FoodInfoActivity.this.finish();
            }
        });
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int food_id=getIntent().getIntExtra("food_id",0);
                collect(String.valueOf(food_id));
            }
        });

    }

    @Override
    void initData() {
        int food_id=getIntent().getIntExtra("food_id",0);

        showFoodInfo(String.valueOf(food_id));
        showComments(String.valueOf(food_id));
        iscollected(String.valueOf(food_id));
    }

    //获取当前店铺菜单
    private void showFoodInfo(String food_id){
        ShopModel shopModel=new ShopModel();
        shopModel.getFoodById(food_id,this);
    }
    //获取当前菜单评论
    private void showComments(String food_id){
        ShopModel shopModel=new ShopModel();
        shopModel.getAllUserComment(food_id,this);
    }
    //判断是否收藏
    private void iscollected(String food_id){
        ShopModel shopModel=new ShopModel();
        shopModel.isCollected(getUser_id(),food_id,"1",this);
    }
    //取消或收藏菜单
    private void collect(String food_id){
        ShopModel shopModel=new ShopModel();
        shopModel.userCollectFood(getUser_id(),food_id,this);
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constants.GETFOODBYID:
                FoodBean foodBean=(FoodBean)o;
                if(!foodBean.getPic().equals("")){
                    Picasso.get().load(Constants.BASE_URL+foodBean.getPic()).into(foodinfo_pic);
                }
                foodinfo_price.setText(foodBean.getPrice()+"元");
                foodinfo_intro.setText("简介:"+foodBean.getIntro());
                foodinfo_name.setText(foodBean.getFoodname());
                break;
            case Constants.GETALLCOMMENTS:
                foodInfoAdapter=new FoodInfoAdapter(FoodInfoActivity.this,(List<CommentBean>)o,R.layout.foodinfo_item);
                foodinfo_comments.setAdapter(foodInfoAdapter);
                break;
            case Constants.ISCOLLECTED:
                result = ((CollectResultBean)o).getCollected().toString();
                if(result.equals("0")){
                    imageButton.setImageResource(R.drawable.ic_favorite_border);
                }
                else {
                    imageButton.setImageResource(R.drawable.ic_favorite);
                }
                break;
            case Constants.USERCOLLECTFOOD:
                if(((ResultBean)o).getSuccess().equals("1")){
                    if (result.equals("0")) {
                        imageButton.setImageResource(R.drawable.ic_favorite);
                        Toast.makeText(FoodInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        imageButton.setImageResource(R.drawable.ic_favorite_border);
                        Toast.makeText(FoodInfoActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                }
                else {
                    Toast.makeText(FoodInfoActivity .this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;

        }

    }

    @Override
    public void onFail() {

    }

}

